package com.tdk.wifidirectserver.socket.server


import android.util.Log
import com.tdk.wifidirectserver.socket.client.Client
import com.tdk.wifidirectserver.socket.client.IClientListener
import java.net.ServerSocket
import java.util.concurrent.CopyOnWriteArrayList


/**
 * socket服务端管理类
 * @property TAG String
 * @property mServer ServerSocket? socket服务端
 * @property mIServerListener IServerActionListener? server回调接口
 * @property mAcceptThread AcceptThread? 轮询等待客户端连接的线程
 */
class Server : IServerAction {
    private val TAG = "Server"
    private var mServer: ServerSocket? = null
    private var localPort: Int = 0
    private var serverName: String? = null

    fun getServerName(): String? {
        return serverName
    }

    /**
     * 客户端集合
     */
    private var mClientList = CopyOnWriteArrayList<Client>()
    private var mIServerListener: IServerListener? = null
    private var serverThread: ServerThread? = null
    fun setIServerListener(iServerListener: IServerListener?): Server {
        mIServerListener = iServerListener
        return this
    }


    fun createServer(port: Int? = null, name: String) {
        try {
            mServer = if (port == null) {
                ServerSocket(0)
            } else {
                ServerSocket(port)
            }
            mServer!!.reuseAddress = true
            localPort = mServer!!.localPort
            mServer!!.soTimeout = 0
            serverName = name
            mIServerListener?.onServerCreateSuccess(localPort)
            serverThread = ServerThread(mServer!!, mIServerListener)
            serverThread!!.start()
        } catch (e: Exception) {
            e.printStackTrace()
            mIServerListener?.onServerCreateFail(localPort)
        }

    }

    fun sendData(data: String?, client: Client) {
        if (serverThread == null || client == null || data == null) {
            Log.d(TAG, "sendData  serverThread is null  or client is null or data is null")
            return
        }
        serverThread!!.sendData(data.toByteArray(), client)
    }

    override fun sendData(data: ByteArray?, client: Client) {
        if (serverThread == null) {
            Log.d(TAG, "sendData  serverThread is null")
            return
        }
        serverThread!!.sendData(data, client)
    }

    override fun broadcastData(data: ByteArray?) {
        if (serverThread == null) {
            Log.d(TAG, "broadcastData  serverThread is null")
            return
        }
        serverThread!!.broadcastData(data)
    }


    fun broadcastData(data: String?) {
        if (serverThread == null || data == null) {
            Log.d(TAG, "broadcastData  serverThread is null")
            return
        }
        serverThread!!.broadcastData(data.toByteArray())
    }

    override fun closeClient(client: Client) {
        if (serverThread == null) {
            Log.d(TAG, "closeClient  serverThread is null")
            return
        }
        serverThread!!.closeClient(client)
    }

    override fun closeServer() {
        if (serverThread == null) {
            Log.d(TAG, "closeClient  serverThread is null")
            return
        }
        mClientList.forEach {
            it.disconnect()
        }
//        mServer?.close()
        serverThread!!.closeServer()
    }

}